<?php
	/**
	 * 商品详细页面控制器
	 */
	 class DetailsController extends CommonController{
	 	
		private $model;
		
		public function __init(){
			parent::__init();
			$this->model = K('Goods');
		}
		
		/**
		 * AJAX查询当前商品库存
		 */
		 public function ajaxStock(){
		 	if(!IS_AJAX) $this->error('非常请求');	
		 	$combine = Q('post.combine','','strval');
			$stock = K('GoodsList')->getStock($combine);
			if($stock){
				$this->ajax(array('message'=>'有货','data'=>$stock,'status'=>1));
			}else{
				$this->ajax(array('message'=>'缺货','status'=>0));
			}
		 }
		
		
	 	/**
		 * 展示当前gid的商品信息
		 */
		public function index(){
				 	//当前商品的Gid
	 		$gid = Q('get.gid',0,'intval');
//			if(!$gid){
//				//如果为0那么就跳转到list页面中
//				go('List/index');
//				return false;
//			}
			$this->getComments($gid);
			//添加点击率+1
			$this->model->inc('goods_click',"gid={$gid}",1);
			//获得当前商品的详细信息、商品信息、库存信息、类型属性
//			var_dump($this->model->showGoods($gid))
			
			//热销商品取8条
			$hotGoods = M()->join('__goods__ g JOIN __order_list__ ol ON g.gid=ol.goods_gid ')->field('ol.goods_gid,sum(ol.goods_num) as sum,goods_name,goods_name,shop_price,goods_listimg')->group('ol.goods_gid')->order('sum DESC')->limit(8)->all();
			
			$this->assign('hotgoods',$hotGoods);
			
			//显示已经回复了的提问信息
			$this->goodsQuest($gid);
			
			$goods = $this->model->showGoods($gid);
			//当前商品的分类ID
			$cid = $goods['goods']['goods_cateid'];
			$mbx = K('Category')->getFather($cid);
			
			$this->assign('mbx',$mbx);
			$this->assign('goods',$goods);
			
			$this->display();
		}
		
		/**
		 * 查询当前商品的所有评论
		 */
		 public function getComments($gid,$cpage=null){
		 	$total2 = K('GoodsComment')->where('goods_gid='.$gid)->order('gc_time DESC')->count();
			$this->assign('total2',$total2);
			$page = new Page($total2,8,10);
			$comments = K('GoodsComment')->where('goods_gid='.$gid)->order('gc_time DESC')->limit($page->limit())->all();
			foreach($comments as $k=>$v){
				$comments[$k]['username'] = K('Users')->where("uid={$v['users_uid']}")->getField('username');
				$comments[$k]['combinestr'] = M()->join('__goods_order__ go JOIN __order_list__ ol ON ol.order_oid=go.oid')->where("go.users_uid={$v['users_uid']} AND ol.goods_gid={$v['goods_gid']}")->order('go.order_time DESC')->limit(1)->getField('ol.goods_combinestr');
				$comments[$k]['combinestr'] = explode(',',$comments[$k]['combinestr']);
				$comments[$k]['username'] = substr_replace($comments[$k]['username'],'***',0,3);
				$comments[$k]['username'] = substr_replace($comments[$k]['username'],'**',-2);
				$comments[$k]['gc_time'] = date('Y-m-d H:i:s',$comments[$k]['gc_time']);
			}
			
			$this->assign('comments',$comments);
			$this->assign('page2',$page->show(4));
			if($cpage){
				return $this->display(null,-1,null,'text/html',false);
			}
		 }
		 
		
		
		
		
		/**
		 * 显示当前商品的提问内容
		 */
		 public function goodsQuest($gid,$cpage=null){
		 	//获得当前商品提问数
		 	
		 	$total = M()->field('g.gid,goods_name,quest_content,answer,qa.quest_time,u.username')
						->join('__questanswer__ qa JOIN __goods__ g JOIN __users__ u ON qa.goods_gid=g.gid AND u.uid=qa.users_uid')
						->where("qa.answer!='' AND qa.goods_gid={$gid}")
						->order('qa.quest_time DESC')->count();
			$this->assign('total',$total);
		 	$page = new Page($total,8,10);
			//获得当前商品提问内容
		 	$quest = M()->field('g.gid,goods_name,quest_content,answer,qa.quest_time,u.username')
						->join('__questanswer__ qa JOIN __goods__ g JOIN __users__ u ON qa.goods_gid=g.gid AND u.uid=qa.users_uid')
						->where("qa.answer!='' AND qa.goods_gid={$gid}")
						->order('qa.quest_time DESC')->limit($page->limit())->all();
			$this->assign('quest',$quest);
			$this->assign('page',$page->show(4));
			if($cpage){
				return $this->display(null,-1,null,'text/html',false);
			}
		 }
		
		/**
		 * ajax返回提问内容
		 */
		 public function ajaxQa(){
		 	$pageNum = Q('get.page',0,'intval');
			$gid = 	Q('get.gid',0,'intval');
			$this->ajax($this->goodsQuest($gid,$pageNum));
		 }
		
		/**
		 * ajax获得提问
		 */
		 public function ajaxComm(){
		 	$pageNum = Q('get.page',0,'intval');
			$gid = 	Q('get.gid',0,'intval');
			$this->ajax($this->getComments($gid,$pageNum));
			
		 }
		
		/**
		 * 判断用户是否登陆
		 */
		 public function ajaxIsLogin(){
		 	if(!IS_AJAX) $this->error('非法请求');
			if(session('?uid')){
				$this->ajax(array('status'=>1));
			}else{
				$this->ajax(array('message'=>'您还没有登陆','status'=>0));
			}
		 }
		
		 /**
		  * 提交相应商品的问题
		  */
		  public function addQeust(){
		  	if(!IS_AJAX) $this->error('非法请求');
			$quest = K('Quest');
			if($quest->addQuest()){
				$this->ajax(array('message'=>'提问成功请等待回复','status'=>1));
			}else{
				$this->ajax(array('message'=>$quest->error,'status'=>0));
			}
		  }
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	 }
	